package org.example.book.service;

import org.example.book.dao.BookDao;
import org.example.book.enums.BookStatus;
import org.example.book.mapper.BookMapper;
import org.example.book.model.BookInfo;
import org.example.book.model.PageRequest;
import org.example.book.model.PageResult;
import org.example.book.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Component
public class BookService {
    @Autowired
    private BookDao bookDao;
    @Autowired
    private BookMapper bookMapper;



  /*  public List<BookInfo> getBookList(){
     //   BookDao bookDao = new BookDao(); 你这个地方new了 ！
        List<BookInfo> bookInfos = bookDao.mockData();
        for(BookInfo bookInfo: bookInfos){
            if(bookInfo.getStatus()==2){
                bookInfo.setStatusCN("不可借阅");
            }else{
                bookInfo.setStatusCN("可借阅");
            }
        }
        return bookInfos;
     }*/

    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {

        //1.获取总记录数
        Integer count = bookMapper.count();
        //2.获取当前页记录
        List<BookInfo> bookInfos = bookMapper.queryBookByPage(pageRequest
                .getOffset(),pageRequest.getPageSize());
        //3.处理状态
        //4.状态 0-删除 1-可借阅 2- 不可借阅
        for(BookInfo bookInfo : bookInfos){
            bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult(bookInfos,count,pageRequest);
    }


    /*
    * 根据Id查询图书信息
    * */
    public BookInfo queryBookById(Integer bookId) {
        BookInfo bookInfo = bookMapper.queryBookById(bookId);
        bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        return bookInfo;
    }


    public Integer updateBookById(BookInfo bookInfo) {
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer deleteBook(Integer bookId){
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(0);
        return bookMapper.updateBookById(bookInfo);
    }
    public  Integer batchDeleteByIds(List<Integer> ids) {
        return bookMapper.batchDeleteByIds(ids);

    }
}
